import 'package:app/pages/userinfo_module/userinfo_model.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:get/get_state_manager/src/simple/get_controllers.dart';
import 'package:image_pickers/image_pickers.dart';

import '../../common/colors.dart';
import '../../services/address.dart';
import '../../services/dio_manager.dart';
import 'dart:io';
import 'package:get/route_manager.dart';
class UserInfoController extends GetxController{

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;


  TextEditingController editingController = TextEditingController();

  UserinfoModel? model;

  List<Media>? listFilePaths = [];

  /// 获取用户个人信息
  requestDataWithUserInfo() async {
    var json = await DioManager().kkRequest(Address.infoRead, method: 'get');

    UserinfoModel _model = UserinfoModel.fromJson(json);

    model = _model;

    update();
  }

  /// 修改个人信息
  requestDataWithUpdataInfo({var alias_name,var sex,var birthday,var avatar,var contract_address}) async {

    var formData = FormData.fromMap({
      // 'name':alias_name,
      'alias_name':alias_name,
      'sex':sex,
      'birthday':birthday,
      'avatar':avatar,
      'contract_address':contract_address,
    });
    var json = await DioManager().kkRequest(Address.infoWrite,bodyParams: formData);

    if(json['errmsg']=='no'){

    }else{
      BotToast.showText(text: '${json['message']}');
      // Get.back(result: 'refresh');

    }
    requestDataWithUserInfo();

  }

  /// 点击完成
  tapClickDone(){
    requestDataWithUpdataInfo(alias_name:editingController.text,);
    Get.back();
  }
  /// 点击完成
  tapClickAddressDone(){
    requestDataWithUpdataInfo(contract_address:editingController.text,);
    Get.back();
  }

  /// 选择头像
  ///选择图片
  Future<void> selectImages() async {

    print(listFilePaths!.length);
    try {
      // _galleryMode = GalleryMode.image;
      // listFilePaths = await ImagePickers.pickerPaths(
      //   galleryMode: GalleryMode.image,
      //   showGif: false,
      //   selectCount: 1,
      //   showCamera: false,
      //   cropConfig: CropConfig(enableCrop: true, height: 1, width: 1),
      //   compressSize: 300,
      //   uiConfig: UIConfig(
      //     uiThemeColor: AppColor.themeColor,
      //   ),
      // );
      ImagePickers.pickerPaths(
        galleryMode: GalleryMode.image,
        showGif: false,
        selectCount: 1,
        showCamera: false,
        cropConfig: CropConfig(enableCrop: true, height: 1, width: 1),
        compressSize: 300,
        uiConfig: UIConfig(
          uiThemeColor: AppColor.themeColor,
        ),
      ).then((value) {
        listFilePaths!.clear();
        listFilePaths!.addAll(value);

        print(listFilePaths!.length);

        if (listFilePaths!.length > 0) {
          MultipartFile multipartFile = MultipartFile.fromFileSync(
            '${listFilePaths![0].path}',filename: 'avator_userinfo',
          );

          requestDataWithUpdataInfo(avatar: multipartFile);
          listFilePaths!.forEach((media) {
            print('media.path.toString() ==== ${media.path.toString()}');
          });
        }
        update();

      });


    } on PlatformException {}
  }


  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

    print(listFilePaths);
    requestDataWithUserInfo();
  }
}
